'use strict';
const mongoose = require('mongoose');

module.exports={
    list:async function(options={group:'',type:'',minTimeTick:0,maxTimeTick:0}){
        let CensusModel=mongoose.models['Census'];
        let now=new Date().getTime();
        let query={
            timeTick:{$gte:now-86400000,$lt:now}
        };
        if(!!options.group){
            query.group=options.group;
        }
        if(!!options.type){
            query.type=options.type;
        }
        if(!!options.minTimeTick){
            query.timeTick.$gte=parseInt(options.minTimeTick);
        }
        if(!!options.maxTimeTick){
            query.timeTick.$lt=parseInt(options.maxTimeTick);
        }
        let census=await CensusModel.find(query);
        //channels.sort((a,b)=>{return b.sort-a.sort;});
        return census;
    },
    //
    remainList:async function(options={group:'',type:'',minTimeTick:0,maxTimeTick:0}){
        let CensusModel=mongoose.models['Census'];
        let now=new Date().getTime();
        let groups = [];
        let query={
            timeTick:{$gte:now-86400000,$lt:now}
        };
        if(!!options.group){
            groups = options.group.split(',');
            query.group={$in: groups};
        }
        if(!!options.type){
            query.type=options.type;
        }
        if(!!options.minTimeTick){
            query.timeTick.$gte=parseInt(options.minTimeTick);
        }
        if(!!options.maxTimeTick){
            query.timeTick.$lt=parseInt(options.maxTimeTick);
        }
        let census=await CensusModel.find(query);
        //channels.sort((a,b)=>{return b.sort-a.sort;});
        let censusRes = new Array(groups.length);
        for(let i=0;i<censusRes.length;i++){
            censusRes[i] = [];
        }
        for(let i=0;i<census.length;i++){
            censusRes[groups.indexOf(census[i].group)].push(census[i]);
        }
        return censusRes;
    },
}